﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace AdatbazisProjekt
{
    class UserData : DalGen
    {
        public UserData(ref bool Err)
        {
            if (!base.IsConnectCreated())
            {
                Err = !base.MakeConnection();
            }
        }

        public int GetUserByUserName(string userName, string passwd)
        {
            try
			{
				string error = "";
				string cmd = "SELECT dbo.isItGood('"+userName+"','"+passwd+"') AS jo;";
                int priv = 0;
                SqlDataReader reader = mExecuteReader(cmd, ref error, null);
                if ( reader.Read())
				{
                   priv = Convert.ToInt32(reader["jo"].ToString());
                }
				this.CloseConnection();
                if (priv > 0)
                {
                    return priv;
                }
                else
                {
                    return -1;
                }
			}
			catch (Exception)
			{
              
                return -2;
			}
        }


        public void UjUser(string nev, string passwd, int priv , ref bool error)
        {
            // Uj videokartya beszurasat  vezgi az adatbazisba
            try
            {
                error = false;

                List<String> paraNev = new List<string>();
                List<String> paraErt = new List<string>();

                paraNev.Add("@nev");
                paraNev.Add("@priv");
                paraNev.Add("@gyarto");

                paraErt.Add(nev);
                paraErt.Add(priv.ToString());
                paraErt.Add(priv.ToString());

                string err = "";

                OpenConnection();
                SqlTransaction tran = mySqlConn.BeginTransaction(IsolationLevel.ReadUncommitted);
                SqlDataReader reader = mExecuteReader("SELECT * FROM Userek WHERE username = '" + nev + "'",ref err, tran);
                if (reader.Read())
                {
                    tran.Rollback();
                }
                else
                {
                    MyExecuteNonQueryWithParameters("EXEC InsertUser @nev, @priv, @passwd", paraNev, paraErt, tran);
                    tran.Commit(); 
                }
                tran.Dispose();
            }
            catch (Exception)
            {
                error = true;
            }
        }

    }
}
